C# Dispatcher {"The calling thread cannot access this object because a different thread owns it."}

Posted by user359446 on Stack Overflow See other posts from Stack Overflow or by user359446
Published on 2010-06-05T23:17:59Z Indexed on 2010/06/05 23:22 UTC
Read the original article Hit count: 145

Filed under:
|

Hi,

first I need to say that I´m noob with WPF and C#. Application: Create Mandelbrot Image (GUI) My disptacher works perfektly this this case:

  private void progressBarRefresh(){

       while ((con.Progress) < 99)
       {
           progressBar1.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
                {
                    progressBar1.Value = con.Progress;
                }
              ));
       }
  }

I get the Message (Title) when tring to do this with the below code:

bmp = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, stride);

this.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
            {                     
                img.Source = bmp;
                ViewBox.Child = img;  //vllt am schluss
            }
          ));

I will try to explain how my program works. I created a new Thread (because GUI dont response) for the calculation of the pixels and the colors. In this Thread(Mehtod) I´m using the Dispatcher to Refresh my Image in the ViewBox after the calculations are ready.

When I´m dont put the calculation in a seperate Thread then I can refresh or build my Image.

PS: Sry for my bad english

© Stack Overflow or respective owner

Related posts about c#

Related posts about dispatcher